<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,minimal-ui:ios">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="reset.css">
  <title>Document</title>
</head>

<body>
  <canvas id="canvas"></canvas>
  <script>
    let canvas = document.getElementById('canvas');
    let ctx = canvas.getContext('2d');

    canvas.width = document.documentElement.clientWidth
    canvas.height = document.documentElement.clientHeight

    function drawLine(x1, y1, x2, y2) {
      ctx.lineWidth = 10;
      ctx.lineJoin = "round"
      ctx.beginPath();
      ctx.moveTo(x1, y1);
      ctx.lineTo(x2, y2);
      ctx.closePath();
      ctx.stroke();
    }


    ctx.fillStyle = 'black';

    let flag = false;
    let last

    if ("ontouchstart" in document.documentElement) {

      canvas.ontouchstart = (e) => {
        last = [e.touches[0].clientX, e.touches[0].clientY]
      }

      canvas.ontouchmove = (e) => {
        let x = e.touches[0].clientX
        let y = e.touches[0].clientY
        drawLine(last[0], last[1], x, y);
        last = [x, y]
      }
    } else {
      canvas.onmousedown = (e) => {
        flag = true,
          last = [e.clientX, e.clientY]
      }

      canvas.onmouseup = () => flag = false

      canvas.onmousemove = (e) => {

        if (flag) {
          drawLine(last[0], last[1], e.clientX, e.clientY)
        }
        last = [e.clientX, e.clientY]
      }
    }


  </script>
</body>

</html>